package ihm;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import metier.Consultation;
import metier.Hospitalisation;
import metier.Patient;
import metier.Praticien;

import com.toedter.calendar.JDateChooser;

import dao.GestionDonnees;
import dao.dao_Hospitalisation;
import dao.dao_Praticien;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.DefaultComboBoxModel;

public class GestionConsultation extends JInternalFrame implements ActionListener, ListSelectionListener, ItemListener, PropertyChangeListener
{
	private JLabel lblNouvelleConsultation;
	private JSeparator separator;
	private JLabel lblRenseignezLes;
	private JList listPatient;
	private JScrollPane scrollPane;
	
	private dao_Hospitalisation sqlHospit = new dao_Hospitalisation();
	private dao_Praticien sqlPraticien = new dao_Praticien(); 
	private GestionDonnees gestionDonnees = new GestionDonnees();
	private DefaultListModel listModel = new DefaultListModel();
	private JLabel lblChoisirUn;
	private JComboBox cbPraticien;
	private JLabel lblDtail;
	private JTextField txtDate;
	private JTextArea txtDescriptif;
	private JButton btnQuitter;
	private JLabel lblDate;
	private JLabel lblDescriptif;
	private JButton btnValider;
	private JScrollPane scrollPane_1;
	private JLabel lblHeure;
	private JLabel lblhhmm;
	private JButton btnAccueil;
	private Integer etape=1;
	private JPanel etape2;
	private JPanel etape3;
	private JLabel label;
	private JDateChooser dateChooser;
	private JComboBox cbH;
	private JComboBox cbM;
	private JLabel label_1;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					GestionConsultation frame = new GestionConsultation();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GestionConsultation()
	{
		// mise en forme 
				setRootPaneCheckingEnabled(false);
				javax.swing.plaf.InternalFrameUI ui = this.getUI();
				((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);
				setBorder(null);
				setFrameIcon(null);
				setRootPaneCheckingEnabled(false);
				setBounds(0, 0, 840, 580);
				getContentPane().setLayout(null);
				
				etape3 = new JPanel();
				etape3.setBounds(0, 321, 840, 118);
				getContentPane().add(etape3);
				etape3.setLayout(null);
				etape3.setVisible(false);
				
				lblDtail = new JLabel("3- D\u00E9tails :");
				lblDtail.setBounds(36, 0, 528, 22);
				etape3.add(lblDtail);
				lblDtail.setFont(new Font("Tahoma", Font.BOLD, 18));
				
				txtDescriptif = new JTextArea();
				txtDescriptif.setBounds(139, 374, 607, 50);
				
				scrollPane_1 = new JScrollPane(txtDescriptif);
				scrollPane_1.setBounds(118, 55, 607, 50);
				etape3.add(scrollPane_1);
				
				lblDate = new JLabel("Date :");
				lblDate.setBounds(36, 31, 72, 22);
				etape3.add(lblDate);
				lblDate.setHorizontalAlignment(SwingConstants.RIGHT);
				lblDate.setFont(new Font("Tahoma", Font.PLAIN, 15));
				
				lblHeure = new JLabel("Heure :");
				lblHeure.setBounds(292, 31, 72, 22);
				etape3.add(lblHeure);
				lblHeure.setHorizontalAlignment(SwingConstants.RIGHT);
				lblHeure.setFont(new Font("Tahoma", Font.PLAIN, 15));
				
				lblhhmm = new JLabel("(HH:MM)");
				lblhhmm.setBounds(475, 31, 61, 22);
				etape3.add(lblhhmm);
				lblhhmm.setHorizontalAlignment(SwingConstants.RIGHT);
				lblhhmm.setFont(new Font("Tahoma", Font.PLAIN, 15));
				
				txtDate = new JTextField();
				txtDate.setEditable(false);
				txtDate.setBounds(118, 31, 97, 22);
				etape3.add(txtDate);
				txtDate.setColumns(10);
				
				lblDescriptif = new JLabel("Descriptif :");
				lblDescriptif.setBounds(37, 55, 70, 22);
				etape3.add(lblDescriptif);
				lblDescriptif.setFont(new Font("Tahoma", Font.PLAIN, 15));
				lblDescriptif.setHorizontalAlignment(SwingConstants.RIGHT);
				
				dateChooser = new JDateChooser();
				dateChooser.addPropertyChangeListener(this);
				dateChooser.setBorder(null);
				dateChooser.setBounds(220, 31, 23, 22);
				etape3.add(dateChooser);
				
				cbH = new JComboBox();
				for (int i=0 ; i<=23 ; i++)
				{
					if (i<10)
					{
						cbH.addItem("0" + i);
					}
					else
					{
						cbH.addItem(i);
					}
				}
				cbH.setBounds(376, 31, 40, 22);
				etape3.add(cbH);
				
				cbM = new JComboBox();
				for (int i=0 ; i<=59 ; i++)
				{
					if (i<10)
					{
						cbM.addItem("0"+i);
					}
					else
					{
						cbM.addItem(i);
					}
	
				}
				cbM.setBounds(425, 31, 40, 22);
				etape3.add(cbM);
				
				label_1 = new JLabel(":");
				label_1.setHorizontalAlignment(SwingConstants.RIGHT);
				label_1.setFont(new Font("Tahoma", Font.PLAIN, 15));
				label_1.setBounds(418, 31, 5, 22);
				etape3.add(label_1);
				
				etape2 = new JPanel();
				etape2.setBorder(null);
				etape2.setBounds(0, 257, 840, 62);
				getContentPane().add(etape2);
				etape2.setLayout(null);
				etape2.setVisible(false);
				
				lblChoisirUn = new JLabel("2- Choisir un praticien :");
				lblChoisirUn.setBounds(35, 0, 528, 22);
				etape2.add(lblChoisirUn);
				lblChoisirUn.setFont(new Font("Tahoma", Font.BOLD, 18));
				
				cbPraticien = new JComboBox();
				cbPraticien.addItem("Choisir un praticien");
				for (Praticien unPrat : sqlPraticien.getLesPraticiens())
				{
					cbPraticien.addItem(unPrat);
				}
				cbPraticien.addItemListener(this);
				cbPraticien.setBounds(62, 30, 303, 22);
				etape2.add(cbPraticien);
				
				
				lblNouvelleConsultation = new JLabel("Nouvelle consultation");
				lblNouvelleConsultation.setHorizontalAlignment(SwingConstants.CENTER);
				lblNouvelleConsultation.setForeground(new Color(0, 0, 102));
				lblNouvelleConsultation.setFont(new Font("Comic Sans MS", Font.PLAIN, 35));
				lblNouvelleConsultation.setBounds(0, 0, 840, 50);
				getContentPane().add(lblNouvelleConsultation);
				
				separator = new JSeparator();
				separator.setBounds(232, 75, 372, 16);
				getContentPane().add(separator);
				
				lblRenseignezLes = new JLabel("1- Choisir un patient :");
				lblRenseignezLes.setFont(new Font("Tahoma", Font.BOLD, 18));
				lblRenseignezLes.setBounds(35, 104, 528, 22);
				getContentPane().add(lblRenseignezLes);
				
				for (Hospitalisation uneHospit : sqlHospit.getLesHospitsEnCours())
				{
					listModel.addElement(uneHospit.getUnPatient());
				}
				
				listPatient = new JList(listModel);
				listPatient.addListSelectionListener(this);
				listPatient.setBounds(38, 179, 767, 152);
				
				scrollPane = new JScrollPane(listPatient);
			
				
				scrollPane.setBounds(59, 134, 717, 110);
				getContentPane().add(scrollPane);
				
				btnQuitter = new JButton("Quitter");
				btnQuitter.addActionListener(this);
				btnQuitter.setBounds(682, 452, 97, 25);
				getContentPane().add(btnQuitter);
				
				btnValider = new JButton("Valider");
				btnValider.addActionListener(this);
				btnValider.setBounds(573, 452, 97, 25);
				getContentPane().add(btnValider);
				
				btnAccueil = new JButton("");
				btnAccueil.setIcon(new ImageIcon("logo_accueil.png"));
				btnAccueil.addActionListener(this);
				btnAccueil.setBorder(BorderFactory.createLineBorder(Color.white));
				btnAccueil.setBounds(22, 13, 50, 50);
				getContentPane().add(btnAccueil);	
				
				label = new JLabel("");
				label.setBounds(120, 62, 100, 16);
				getContentPane().add(label);
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource()==btnQuitter)
		{
			System.exit(0);
		}
		if (evt.getSource()==btnValider)
		{
			if (listPatient.getSelectedIndex()==-1 || cbPraticien.getSelectedItem()=="Choisir un praticien" || cbPraticien.getSelectedIndex()==-1 || txtDate.getText().length()==0 || txtDescriptif.getText().length()==0)
			{
				JOptionPane.showMessageDialog(null, "Certain champs sont menquants !","Consultation",JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				if (gestionDonnees.estValideDateFr(txtDate.getText())==false)
				{
					JOptionPane.showMessageDialog(null, "La date saisie n'est pas valide ! ","Consultation",JOptionPane.ERROR_MESSAGE);
				}
				else
				{
					String dateTime = txtDate.getText() + " " + cbH.getSelectedItem() + ":" + cbM.getSelectedItem();
					if (gestionDonnees.estValideDateTimeFr(dateTime)==false)
					{
						JOptionPane.showMessageDialog(null, "L'heure saisie n'est pas valide ! ","Consultation",JOptionPane.ERROR_MESSAGE);
					}
					else
					{
						//if (txtDate.getText().substring(3,1)!="/")
						//System.out.println(txtDate.getText().substring(3,1));
						Patient unPatient = (Patient) listPatient.getSelectedValue();
						Praticien unPraticien = (Praticien) cbPraticien.getSelectedItem();
						Hospitalisation uneHospitalisation = new Hospitalisation();
						for (Hospitalisation uneHospit : sqlHospit.getLesHospitsEnCours())
						{
							if (uneHospit.getUnPatient()==unPatient)
							{
								uneHospitalisation=uneHospit;
							}
						}
						java.util.Date uneDate = gestionDonnees.dateTimeAng(gestionDonnees.dateTimeVersStrAng(gestionDonnees.dateTimeFr(dateTime))); 
						
						Consultation uneConsultation = new Consultation(unPraticien, uneHospitalisation, uneDate, txtDescriptif.getText());
						sqlHospit.ajouterConsultation(uneConsultation);
						System.out.println(uneConsultation.getUneHospitalisation().getUnPatient()); 
						
					}
				}
			}
		}
		if (evt.getSource()==btnAccueil)
		{
			Accueil.retourAccueil();
		}
	
	}

	
	public void valueChanged(ListSelectionEvent evt) {
		if (evt.getSource()==listPatient)
		{
			etape2.setVisible(true);
		}
	}
	public void itemStateChanged(ItemEvent e) {
		if (e.getSource()==cbPraticien)
		{
			etape3.setVisible(true);
		}
		
	}
	public void propertyChange(PropertyChangeEvent evt) {
	
			//label.setText(gestionDonnees.dateVersStrFr(dt.getDate())); 
		
		
		if (evt.getSource()==dateChooser && dateChooser.getDate()!=null)
		{
			txtDate.setText(gestionDonnees.dateVersStrFr(dateChooser.getDate()));
			
		}
	}
}
